//
//  BirthdayViewController.m
//  PhotoAnnotate
//
//  Created by Bassem El-Tamimi on 09.03.11.
//  Copyright 2011 ePapyrus GmbH. All rights reserved.
//

#import "BirthdayViewController.h"
#import "Insurance.h"


@implementation BirthdayViewController

@synthesize datePicker;
@synthesize insuranceData;

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = NSLocalizedString(@"INSURANCE_BIRTH_DATE_TITLE", @"Birthday");
	self.navigationController.navigationBar.translucent = NO;
	self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
	
	[birthdayLabel setText:NSLocalizedString(@"INSURANCE_BIRTH_DATE_LABEL", @"Birthday")];
	
	if ([[self.insuranceData.birthDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]] compare:[NSDate dateWithTimeIntervalSince1970:[[NSTimeZone localTimeZone] secondsFromGMT]]] != NSOrderedSame) {
		datePicker.date = self.insuranceData.birthDate;
	} else {
		NSCalendar *gregorian = [[NSCalendar alloc]  initWithCalendarIdentifier:NSGregorianCalendar];
		NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
		NSInteger year = [dateComponents year];
		NSInteger month = [dateComponents month];
		NSInteger day = [dateComponents day];
		
		if (day == 29 && month == 2) {
			day = 1;
			month = 3;
		}
		
		year = year - 18;
				
		[dateComponents setYear:year];
		[dateComponents setMonth:month];
		[dateComponents setDay:day];
		
		NSDate *lastBirthday = [gregorian dateFromComponents:dateComponents];
		
		datePicker.date = lastBirthday;
		datePicker.maximumDate = lastBirthday;
        
        datePicker.calendar = gregorian;
		[gregorian release];
	}
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)viewWillDisappear:(BOOL)animated
{	
	[super viewWillDisappear:animated];
	
	// save the date
	self.insuranceData.birthDate = datePicker.date;
	[self.insuranceData updatePropertyListData];
	[self.insuranceData saveChanges];
	
	[self.navigationController setNavigationBarHidden:YES animated:NO];
	// check if datePicker is todays date.
	
	if ([datePicker.date compare:datePicker.maximumDate] == NSOrderedSame) {
		// If so ask user if the device was purchased today			
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"INSURANCE_BIRTH_DATE_IS_TODAY_INQUIRY_ALERT", @"Birthday Today?") 
														message:NSLocalizedString(@"INSURANCE_BIRTH_DATE_IS_TODAY_INQUIRY", @"Is your birthday actually today?") 
													   delegate:self.insuranceData 
											  cancelButtonTitle:NSLocalizedString(@"INSURANCE_BIRTH_DATE_IS_TODAY_INQUIRY_NO_BUTTON_TITLE", @"NO") 
											  otherButtonTitles:NSLocalizedString(@"INSURANCE_BIRTH_DATE_IS_TODAY_INQUIRY_YES_BUTTON_TITLE", @"YES"), nil];
		alert.tag = 2;
		[alert show];	
		[alert release];
		
	}
}


- (IBAction)dateChanged:(id)sender
{
	if ([datePicker.date compare:datePicker.maximumDate] == NSOrderedAscending) {
		self.insuranceData.birthDate = datePicker.date;
		[self.insuranceData updatePropertyListData];
		[self.insuranceData saveChanges];
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
